<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>日期计算</title>
</head>
<body>
  <script>
    var date_week=function(day){ return ['日','一','二','三','四','五','六'][day] };
    var date_instance=function(date){ var [years,year,month,week,day,hour,minute,second,time]=[date.getFullYear(),date.getYear()%100,date.getMonth()+1,date_week(date.getDay()),date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds(),date.getTime()]; return {years,year,month,week,day,hour,minute,second,time}; };
    var date_format=function(date,str){
      var {years,year,week,month,day,hour,minute,second}=date_instance(date);
      return str.replace(/YYYY/,years).replace(/YY/,year>9?year:'0'+year).replace(/MM/,month>9?month:'0'+month).replace(/M/g,month).replace(/W/g,week).replace(/DD/,day>9?day:'0'+day).replace(/D/g,day).replace(/HH/,hour>9?hour:'0'+hour).replace(/H/g,hour).replace(/mm/,minute>9?minute:'0'+minute).replace(/m/g,minute).replace(/SS/,second>9?second:'0'+second).replace(/S/g,second);
    };
    console.log(date_format(new Date(),"YYYY-MM-DD HH:mm:SS"));
    var date_add=function(date,type,value){
      var {years,year,month,week,day,hour,minute,second,time}=date_instance(date);
      var dtTmp=date;
      console.log(date_instance(date),time+(86400000*value));
      switch (type){
      case 'S':return new Date(time+(1000*value));
      case 'm':return new Date(time+(60000*value));
      case 'H':return new Date(time+(3600000*value));
      case 'D':return new Date(time+(86400000*value));
      case 'W':return new Date(time+((86400000*7)*value));
      case 'Q':return new Date(years, (month)+value*3, day, hour, minute, second);
      case 'M':return new Date(years, (month)+value, day, hour, minute, second);
      case 'Y':return new Date((years+value), month, day, hour, minute, second);
      }
    };
    console.log(date_add(new Date(),"D",6));
  </script>
</body>
</html>